from PyQt5.QtWidgets import *

import common
from UI.mainwindow import *
from data_manage import read_json
from .show_tab_com import ShowTabCom
from .show_tab_pcan import ShowTabPCan
from .show_tab_udp import ShowTabUdp
from .show_tab_motor_driver import ShowTabMotorDriver
from .show_tab_motor_cut import ShowTabMotorCut
from .show_tab_base import ShowTabBase
from .show_tab_sensor import ShowTabSensor
from .show_tab_light import ShowTabLight
from common.common import Common
import time
import logging


class ShowWindow(QMainWindow):
    def __init__(self, name='dlg'):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.init_ui(name)
        self.dict_info = read_json.ReadJson("info.json").paste_json()
        self.logger = logging.getLogger(__name__)

    def init_ui(self, name):
        self.ui.setupUi(self)
        self.setWindowTitle(name)
        ShowTabCom(self.ui, self).init_ui()
        ShowTabPCan(self.ui, self).init_ui()
        ShowTabUdp(self.ui, self).init_ui()
        Common().set_show_tab_base(ShowTabBase(self.ui, self))
        Common().get_show_tab_base().init_ui()
        Common().set_show_tab_motor_driver(ShowTabMotorDriver(self.ui, self))
        Common().get_show_tab_motor_driver().init_ui()
        Common().set_show_tab_motor_cut(ShowTabMotorCut(self.ui, self))
        Common().get_show_tab_motor_cut().init_ui()
        Common().set_show_tab_sensor(ShowTabSensor(self.ui, self))
        Common().get_show_tab_sensor().init_ui()
        Common().set_show_tab_window(self)
        Common().set_show_tab_light(ShowTabLight(self.ui, self))
        Common().get_show_tab_light().init_ui()
        self.init_btn_connect()
        self.ui.tabWidget.setCurrentIndex(1)
        self.ui.tab_com.setEnabled(False)
        self.ui.tab_udp.setEnabled(False)
        #self.ui.tab_pcan.setEnabled(False)
        self.get_tab_enable()
        Common.setup_logging()
        pass

    def get_tab_enable(self):
        if self.ui.tab_udp.isEnabled():
            Common().set_connect_type('udp')
        elif self.ui.tab_com.isEnabled():
            Common().set_connect_type('com')
        elif self.ui.tab_pcan.isEnabled():
            Common().set_connect_type('can')

    def init_btn_connect(self):
        # menu bar event
        self.ui.actionVersion.triggered.connect(self.show_version)

    def show_version(self):
        dict_info = read_json.ReadJson("info.json").paste_json()
        if len(dict_info) == 0:
            QMessageBox.warning(self, "Error", "不存在Json文件或文件中没有数据", QMessageBox.Cancel)
        else:
            QMessageBox.about(self, "Version", dict_info["version"])

    def closeEvent(self, event):
        reply = QMessageBox.question(self, 'Message', '确定退出程序？',
                                     QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            if Common().get_can_bus() is not None:
                Common().get_can_bus().send_data(0x226, [0xA5, 0x01, 0x50, 0x00, 0x00, 0x00, 0x00, 0xA5])
                time.sleep(0.08)
                Common().get_can_bus().send_data(0x228, [0xA5, 0x01, 0x70, 0x00, 0x00, 0x00, 0x00, 0xA5])
                time.sleep(0.08)
                Common().get_can_bus().send_data(0x22A, [0xA5, 0x01, 0x90, 0x00, 0x00, 0x00, 0x00, 0xA5])
                time.sleep(0.08)
                Common().get_can_bus().send_data(0x22C, [0xA5, 0x01, 0xA0, 0x00, 0x00, 0x00, 0x00, 0xA5])
                time.sleep(0.08)
                Common().get_can_bus().send_data(0x224, [0xA5, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0xA5])
                time.sleep(0.08)
                Common().set_thread_recv(False)
            elif Common().get_connect_type() == "udp":
                Common().set_thread_recv(False)
                Common().get_udp_socket().udp_close()
            event.accept()
        else:
            event.ignore()

    @staticmethod
    def update_left_driver_data(key, value):
        sig_num = -1
        if key == "left_driver_speed":
            Common().get_show_tab_motor_driver().update_left_driver(value, 0, 1)
            # if value > 0:
            #     Common().get_show_tab_motor().update_left_driver(1, 0, 1)
            #     Common().get_show_tab_motor().update_left_driver(value, 0, 2)
            # else:
            #     Common().get_show_tab_motor().update_left_driver(0, 0, 1)
            #     Common().get_show_tab_motor().update_left_driver(value, 0, 2)
            sig_num = 1
        elif key == "left_driver_current":
            Common().get_show_tab_motor_driver().update_left_driver(value, 1, 1)
            sig_num = 1
        elif key == "left_driver_torque":
            Common().get_show_tab_motor_driver().update_left_driver(value, 2, 1)
            sig_num = 1
        elif key == "left_driver_odo":
            Common().get_show_tab_motor_driver().update_left_driver(value, 3, 1)
            sig_num = 1
        elif key == "left_driver_error_code":
            Common().get_show_tab_motor_driver().update_left_driver(value, 4, 1)
            sig_num = 1
        return sig_num

    @staticmethod
    def update_right_driver_data(key, value):
        sig_num = -1
        if key == "right_driver_speed":
            Common().get_show_tab_motor_driver().update_right_driver(value, 0, 1)
            # if value > 0:
            #     Common().get_show_tab_motor().update_right_driver(1, 0, 1)
            #     Common().get_show_tab_motor().update_right_driver(value, 0, 2)
            # else:
            #     Common().get_show_tab_motor().update_right_driver(0, 0, 1)
            #     Common().get_show_tab_motor().update_right_driver(value, 0, 2)
            sig_num = 1
        elif key == "right_driver_current":
            Common().get_show_tab_motor_driver().update_right_driver(value, 1, 1)
            sig_num = 1
        elif key == "right_driver_torque":
            Common().get_show_tab_motor_driver().update_right_driver(value, 2, 1)
            sig_num = 1
        elif key == "right_driver_odo":
            Common().get_show_tab_motor_driver().update_right_driver(value, 3, 1)
            sig_num = 1
        elif key == "right_driver_error_code":
            Common().get_show_tab_motor_driver().update_right_driver(value, 4, 1)
            sig_num = 1
        return sig_num

    @staticmethod
    def update_increase_data(key, value):
        sig_num = -1
        if key == "motor_increase_pos":
            Common().get_show_tab_motor_cut().update_increase(value, 0, 1)
            sig_num = 1
        elif key == "motor_increase_current":
            Common().get_show_tab_motor_cut().update_increase(value, 1, 1)
            sig_num = 1
        elif key == "motor_increase_init_pos_adc":
            Common().get_show_tab_motor_cut().update_increase(value, 2, 1)
            sig_num = 1
        elif key == "motor_increase_hall1_pos_adc":
            Common().get_show_tab_motor_cut().update_increase(value, 3, 1)
            sig_num = 1
        elif key == "motor_increase_hall2_pos_adc":
            Common().get_show_tab_motor_cut().update_increase(value, 4, 1)
            sig_num = 1
        return sig_num

    @staticmethod
    def update_cut1_data(key, value):
        sig_num = -1
        if key == "motor_cut1_speed":
            Common().get_show_tab_motor_cut().update_cut1(value, 0, 1)
            # if value > 0:
            #     Common().get_show_tab_motor().update_cut1(1, 0, 1)
            #     Common().get_show_tab_motor().update_cut1(value, 0, 2)
            # else:
            #     Common().get_show_tab_motor().update_cut1(0, 0, 1)
            #     Common().get_show_tab_motor().update_cut1(value, 0, 2)
            sig_num = 1
        elif key == "motor_cut1_current":
            Common().get_show_tab_motor_cut().update_cut1(value, 1, 1)
            sig_num = 1
        elif key == "motor_cut1_torque":
            Common().get_show_tab_motor_cut().update_cut1(value, 2, 1)
            sig_num = 1
        elif key == "motor_cut1_error_code":
            Common().get_show_tab_motor_cut().update_cut1(value, 3, 1)
            sig_num = 1
        return sig_num

    @staticmethod
    def update_cut2_data(key, value):
        sig_num = -1
        if key == "motor_cut2_speed":
            Common().get_show_tab_motor_cut().update_cut2(value, 0, 1)
            # if value > 0:
            #     Common().get_show_tab_motor().update_cut2(1, 0, 1)
            #     Common().get_show_tab_motor().update_cut2(value, 0, 2)
            # else:
            #     Common().get_show_tab_motor().update_cut2(0, 0, 1)
            #     Common().get_show_tab_motor().update_cut2(value, 0, 2)
            sig_num = 1
        elif key == "motor_cut2_current":
            Common().get_show_tab_motor_cut().update_cut2(value, 1, 1)
            sig_num = 1
        elif key == "motor_cut2_torque":
            Common().get_show_tab_motor_cut().update_cut2(value, 2, 1)
            sig_num = 1
        elif key == "motor_cut2_error_code":
            Common().get_show_tab_motor_cut().update_cut2(value, 3, 1)
            sig_num = 1
        return sig_num

    @staticmethod
    def update_cut3_data(key, value):
        sig_num = -1
        if key == "motor_cut3_speed":
            Common().get_show_tab_motor_cut().update_cut3(value, 0, 1)
            # if value > 0:
            #     Common().get_show_tab_motor().update_cut3(1, 0, 1)
            #     Common().get_show_tab_motor().update_cut3(value, 0, 2)
            # else:
            #     Common().get_show_tab_motor().update_cut3(0, 0, 1)
            #     Common().get_show_tab_motor().update_cut3(value, 0, 2)
            sig_num = 1
        elif key == "motor_cut3_current":
            Common().get_show_tab_motor_cut().update_cut3(value, 1, 1)
            sig_num = 1
        elif key == "motor_cut3_torque":
            Common().get_show_tab_motor_cut().update_cut3(value, 2, 1)
            sig_num = 1
        elif key == "motor_cut3_error_code":
            Common().get_show_tab_motor_cut().update_cut3(value, 3, 1)
            sig_num = 1
        return sig_num

    @staticmethod
    def update_sensor_imu_data(self, key, value):
        sig_num = -1
        if key == "imu_linear_v_x":
            Common().get_show_tab_sensor().update_imu_data(value, 0, 1)
            sig_num = 1
        elif key == "imu_linear_v_y":
            Common().get_show_tab_sensor().update_imu_data(value, 1, 1)
            sig_num = 1
        elif key == "imu_linear_v_z":
            Common().get_show_tab_sensor().update_imu_data(value, 2, 1)
            sig_num = 1
        elif key == "imu_angular_v_x":
            Common().get_show_tab_sensor().update_imu_data(value, 3, 1)
            sig_num = 1
        elif key == "imu_angular_v_y":
            Common().get_show_tab_sensor().update_imu_data(value, 4, 1)
            sig_num = 1
        elif key == "imu_angular_v_z":
            Common().get_show_tab_sensor().update_imu_data(value, 5, 1)
            sig_num = 1
        elif key == "imu_roll_angle":
            Common().get_show_tab_sensor().update_imu_data(value, 6, 1)
            sig_num = 1
            self.logger.info("{}:{}".format(key, value))
        elif key == "imu_pitch_angle":
            Common().get_show_tab_sensor().update_imu_data(value, 7, 1)
            sig_num = 1
            self.logger.info("{}:{}".format(key, value))
        elif key == "imu_heading_angle":
            Common().get_show_tab_sensor().update_imu_data(value, 8, 1)
            sig_num = 1
            self.logger.info("{}:{}".format(key, value))
        return sig_num

    @staticmethod
    def update_sensor_rain_lift_ultrasonic_data(key, value):
        sig_num = -1
        if key == "sensor_rain_adc":
            Common().get_show_tab_sensor().update_rain_data(value, 0, 1)
            sig_num = 1
        elif key == "lift":
            list_bin = list(bin(value).replace('0b', ''))
            if len(list_bin) < 8:
                loop = 8 - len(list_bin)
                for i in range(loop):
                    list_bin.insert(0, '0')
            if list_bin[7] == "1":
                Common().get_show_tab_sensor().update_lift_data(1, 0, 1)
            else:
                Common().get_show_tab_sensor().update_lift_data(0, 1, 1)
            if list_bin[6] == "1":
                Common().get_show_tab_sensor().update_lift_data(1, 0, 1)
            else:
                Common().get_show_tab_sensor().update_lift_data(0, 1, 1)
            sig_num = 1
        elif key == "sensor_ultrasonic1_dis":
            Common().get_show_tab_sensor().update_ultrasonic_data(value, 0, 1)
            sig_num = 1
        elif key == "sensor_ultrasonic2_dis":
            Common().get_show_tab_sensor().update_ultrasonic_data(value, 1, 1)
            sig_num = 1
        elif key == "sensor_ultrasonic3_dis":
            Common().get_show_tab_sensor().update_ultrasonic_data(value, 2, 1)
            sig_num = 1
        elif key == "sensor_ultrasonic4_dis":
            Common().get_show_tab_sensor().update_ultrasonic_data(value, 3, 1)
            sig_num = 1
        elif key == "sensor_ultrasonic5_dis":
            Common().get_show_tab_sensor().update_ultrasonic_data(value, 4, 1)
            sig_num = 1
        elif key == "sensor_ultrasonic6_dis":
            Common().get_show_tab_sensor().update_ultrasonic_data(value, 5, 1)
            sig_num = 1
        elif key == "sensor_ultrasonic7_dis":
            Common().get_show_tab_sensor().update_ultrasonic_data(value, 6, 1)
            sig_num = 1
        elif key == "sensor_ultrasonic8_dis":
            Common().get_show_tab_sensor().update_ultrasonic_data(value, 7, 1)
            sig_num = 1
        return sig_num

    def update_sensor_collide_charger_data(key, value):
        sig_num = -1
        if key == "collide1":
            Common().get_show_tab_sensor().update_collide_data(value, 0, 1)
            sig_num = 1
        elif key == "collide2":
            Common().get_show_tab_sensor().update_collide_data(value, 1, 1)
            sig_num = 1
        elif key == "bluetooth_status":
            Common().get_show_tab_sensor().update_charger_data(value, 0, 1, 1)
            sig_num = 1
        elif key == "bluetooth_connect":
            Common().get_show_tab_sensor().update_charger_data(value, 0, 1, 1)
            sig_num = 1
        elif key == "basestation_charge_current":
            Common().get_show_tab_sensor().update_charger_data(value, 1, 1, 1)
            sig_num = 1
        elif key == "basestation_info_code":
            Common().get_show_tab_sensor().update_charger_data(value, 1, 1, 1)
            sig_num = 1
        return sig_num

    @staticmethod
    def update_get_current_data(key, value):
        sig_num = -1
        if key == "AI_board_current":
            Common().get_show_tab_sensor().update_get_current_data(value, 0, 1)
            sig_num = 1
        elif key == "treble_alarm_5v_current":
            Common().get_show_tab_sensor().update_get_current_data(value, 1, 1)
            sig_num = 1
        elif key == "ultrasonic_control_current":
            Common().get_show_tab_sensor().update_get_current_data(value, 2, 1)
            sig_num = 1
        elif key == "parker_current":
            Common().get_show_tab_sensor().update_get_current_data(value, 3, 1)
            sig_num = 1
        return sig_num

    @staticmethod
    def update_base_data(key, value):
        sig_num = -1
        if key == "battery_error_info":
            Common().get_show_tab_base().update_battery(value, 1, 1)
            sig_num = 1
        elif key == "battery_vol":
            Common().get_show_tab_base().update_battery(value, 2, 1)
            sig_num = 1
        elif key == "battery_electricity":
            Common().get_show_tab_base().update_battery(value, 3, 1)
            sig_num = 1
        elif key == "battery_temperature":
            Common().get_show_tab_base().update_battery(value, 4, 1)
            sig_num = 1
        elif key == "battery_active":
            Common().get_show_tab_base().update_battery(value, 5, 1)
            sig_num = 1
        elif key == "key":
            # byte4
            list_bin = list(bin(int(hex(value & 0xff), 16)).replace('0b', ''))
            if len(list_bin) < 8:
                loop = 8 - len(list_bin)
                for i in range(loop):
                    list_bin.insert(0, '0')
            # byte5
            list_bin5 = list(bin(int(hex(value >> 8 & 0xff), 16)).replace('0b', ''))
            if len(list_bin5) < 8:
                loop = 8 - len(list_bin5)
                for i in range(loop):
                    list_bin5.insert(0, '0')
            # home
            if list_bin[6] == "1":
                Common().get_show_tab_base().update_key(1, 0, 1)
            else:
                Common().get_show_tab_base().update_key(0, 0, 1)
            # start
            if list_bin[7] == "1":
                Common().get_show_tab_base().update_key(1, 1, 1)
            else:
                Common().get_show_tab_base().update_key(0, 1, 1)
            # stop
            if list_bin5[7] == "1":
                Common().get_show_tab_base().update_key(1, 2, 1)
            else:
                Common().get_show_tab_base().update_key(0, 2, 1)
            # ok
            if list_bin[5] == "1":
                Common().get_show_tab_base().update_key(1, 3, 1)
            else:
                Common().get_show_tab_base().update_key(0, 3, 1)
            # return
            if list_bin[4] == "1":
                Common().get_show_tab_base().update_key(1, 4, 1)
            else:
                Common().get_show_tab_base().update_key(0, 4, 1)
            # left
            if list_bin[2] == "1":
                Common().get_show_tab_base().update_key(1, 5, 1)
            else:
                Common().get_show_tab_base().update_key(0, 5, 1)
            # right
            if list_bin[3] == "1":
                Common().get_show_tab_base().update_key(1, 6, 1)
            else:
                Common().get_show_tab_base().update_key(0, 6, 1)
            # up
            if list_bin[0] == "1":
                Common().get_show_tab_base().update_key(1, 7, 1)
            else:
                Common().get_show_tab_base().update_key(0, 7, 1)
            # down
            if list_bin[1] == "1":
                Common().get_show_tab_base().update_key(1, 8, 1)
            else:
                Common().get_show_tab_base().update_key(0, 8, 1)
            sig_num = 1
        return sig_num

    @staticmethod
    def update_light_data(key, value):
        sig_num = -1
        if key == "fill_light1_current":
            Common().get_show_tab_light().update_fill_light1(value, 0, 1)
            sig_num = 1
        elif key == "fill_light2_current":
            Common().get_show_tab_light().update_fill_light2(value, 0, 1)
            sig_num = 1
        elif key == "side_light_current_front_left":
            Common().get_show_tab_light().update_side_light1(value, 0, 1)
            sig_num = 1
        elif key == "side_light_current_front_right":
            Common().get_show_tab_light().update_side_light2(value, 0, 1)
            sig_num = 1
        elif key == "side_light_current_behind_left":
            Common().get_show_tab_light().update_side_light3(value, 0, 1)
            sig_num = 1
        elif key == "side_light_current_behind_right":
            Common().get_show_tab_light().update_side_light4(value, 0, 1)
            sig_num = 1
        return sig_num

    def get_send_response(self, key, value):
        print(key, value)
        for key1, value1 in common.dict_response.items():
            if key == int(key1, 16):
                if value == 0:
                    print(common.dict_response[key1])
                    info = "发送" + common.dict_response[key1] + " 应答失败！！"
                    QMessageBox.warning(self, "Error", info, QMessageBox.Cancel)

    # @staticmethod
    # def update_bluetooth(key, value):
    #     if key == "AI_board_current":
    #         Common().get_show_tab_sensor().update_get_current_data(value, 0, 1)
    #     elif key == "treble_alarm_5v_current":
    #         Common().get_show_tab_sensor().update_get_current_data(value, 0, 2)
    #     elif key == "ultrasonic_control_current":
    #         Common().get_show_tab_sensor().update_get_current_data(value, 0, 3)
    #     elif key == "parker_current":
    #         Common().get_show_tab_sensor().update_get_current_data(value, 0, 4)

    # @staticmethod
    # def update_mess(key, value):
    #     if key == "AI_board_current":
    #         Common().get_show_tab_sensor().update_get_current_data(value, 0, 1)
    #     elif key == "treble_alarm_5v_current":
    #         Common().get_show_tab_sensor().update_get_current_data(value, 0, 2)
    #     elif key == "ultrasonic_control_current":
    #         Common().get_show_tab_sensor().update_get_current_data(value, 0, 3)
    #     elif key == "parker_current":
    #         Common().get_show_tab_sensor().update_get_current_data(value, 0, 4)

    @staticmethod
    def update_tab_motor_driver(key, value):
        if ShowWindow.update_left_driver_data(key, value) == 1:
            return
        if ShowWindow.update_right_driver_data(key, value) == 1:
            return

    @staticmethod
    def update_tab_motor_cut(key, value):
        if ShowWindow.update_increase_data(key, value) == 1:
            return
        if ShowWindow.update_cut1_data(key, value) == 1:
            return
        if ShowWindow.update_cut2_data(key, value) == 1:
            return
        if ShowWindow.update_cut3_data(key, value) == 1:
            return

    @staticmethod
    def update_tab_sensor(self, key, value):
        if ShowWindow.update_sensor_imu_data(self, key, value) == 1:
            return
        if ShowWindow.update_sensor_rain_lift_ultrasonic_data(key, value) == 1:
            return
        if ShowWindow.update_sensor_collide_charger_data(key, value) == 1:
            return
        if ShowWindow.update_get_current_data(key, value) == 1:
            return
        pass

    def update_recv_data(self, dict_data):
        for key, value in dict_data.items():
            # if key == "side_light_a":
            #     Common().get_show_tab_base().update_side_light(value, 0, 1)
            # elif key == "fill_light_a":
            #     Common().get_show_tab_base().update_fill_light(value, 0, 1)
            if ShowWindow.update_tab_motor_driver(key, value) == 1:
                return
            if ShowWindow.update_tab_motor_cut(key, value) == 1:
                return
            if ShowWindow.update_tab_sensor(self, key, value) == 1:
                return
            if ShowWindow.update_base_data(key, value) == 1:
                return
            if ShowWindow.update_light_data(key, value) == 1:
                return
            self.get_send_response(key, value)
